package queue;


import java.util.Stack;

/**
 * https://leetcode.cn/problems/implement-queue-using-stacks/description/
 *
 * 使用两个栈来实现队列，一个专门push，一个pop或者peek的时候先把inStack放入到这个outStack
 */
public class _232_用栈实现队列 {

    Stack<Integer> inStack;
    Stack<Integer> outStack;

    public _232_用栈实现队列() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        checkOutStack();
        return outStack.pop();
    }

    public int peek() {
        checkOutStack();
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.empty() && outStack.empty();
    }

    private void checkOutStack() {
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                int x = inStack.pop();
                outStack.push(x);
            }
        }
    }
}
